using Orchard.ContentManagement.MetaData;
using Orchard.Data.Migration;
using Orchard.Indexing;

namespace Contrib.Chapters {
	public class Migration : DataMigrationImpl {

		public int Create() {
            SchemaBuilder.CreateTable("BookPartRecord",
                table => table
                    .ContentPartRecord()
                    .Column<string>("ChapterTypeName", column => column.WithLength(255))
                );

            SchemaBuilder.CreateTable("ChapterPartRecord", 
				table => table
					.ContentPartRecord()
					.Column<int>("Position")
					.Column<int>("Book_id")
				);

            ContentDefinitionManager.AlterTypeDefinition("Book",
                cfg => cfg
                    .WithPart("BookPart")
                    .WithPart("RoutePart")
                    .WithPart("CommonPart")
                    .WithPart("BodyPart")
                    .WithPart("MenuPart")
                    .Indexed()
            );

			ContentDefinitionManager.AlterTypeDefinition("Chapter",
				cfg => cfg
					.WithPart("ChapterPart")
					.WithPart("RoutePart")
					.WithPart("CommonPart")
					.WithPart("BodyPart")
					.Indexed()
			);

			SchemaBuilder.CreateTable("TocWidgetPartRecord",
				table => table
					.ContentPartRecord()
					.Column<int>("BookPartRecord_id")
				);

			ContentDefinitionManager.AlterTypeDefinition("TableOfContent",
				cfg => cfg
					.WithPart("TocWidgetPart")
					.WithPart("CommonPart")
					.WithPart("WidgetPart")
					.WithSetting("Stereotype", "Widget")
				);

			return 1;
		}
	}
}